home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!xmission!news
- From: tknarr@xmission.com ( Todd Knarr )
- Newsgroups: comp.lang.c++,rb.technical
- Subject: Re: Can copy constructor and operator= share code?
- Date: 2 Mar 1996 18:16:59 GMT
- Organization: Chaos Central
- Message-ID: <4ha3ar$neq@news.xmission.com>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <4h525l$pja@rap.SanDiegoCA.ATTGIS.COM>
- Reply-To: tknarr@xmission.com ( Todd Knarr )
- NNTP-Posting-Host: slc139.xmission.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4h525l$pja@rap.SanDiegoCA.ATTGIS.COM>, dle@costello.SanDiegoCA.ATTGIS.COM (Jake Le) writes:
- >You can call the operator= in your copy constructor.
-
- This is true, but in classes with more complex internal structure you
- probably don't want to do it. When operator= is used, it may need to
- undo parts of an initialized object ( freeing allocated memory and the
- like ) before it can do the assignment. If used within the copy ctor,
- the target will not be initialized unless you take care to initialize
- it first ( most of the time you don't ). If part of operator= involves
- deleting memory pointed to by a data member and that data member contains
- random numbers because it wasn't ever initialized, the results will be
- unpleasant.
-
- I tend to break it down into three routines. One is a private routine
- that just does the "copy an object into the target" part. The copy ctor
- can just call this routine directly. operator= does whatever is needed
- to prepare the target for overwriting, then calls the routine to do the
- copying.
-
- --
- Todd Knarr : tknarr@xmission.com | finger for PGP public key
- | Member, USENET Cabal
-
- Seriously, I don't want to die just yet. I don't care how
- good-looking they are, I! don't! want! to! die!"
- -- Megazone ( UF1 )
-
-